package com.itheima.manager_system.student_service;

import com.itheima.manager_system.domain.Person;
import com.itheima.manager_system.domain.Student;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class ScoreOperater {
    private Scanner sc = new Scanner(System.in);

    public void scoreMenu(ArrayList<Person> list) {
        String select;

        while (true) {
            System.out.println("-------------------------------------------");
            System.out.println("1 录入/修改分数信息  2 查询分数 3 返回上一层 ");
            System.out.println("-------------------------------------------");
            select = sc.next();
            switch (select) {
                case "1":
                    addScore(list);
                    break;
                case "2":
                    allScore(list);
                    break;
                case "3":
                    return;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    private void allScore(ArrayList<Person> list) {
        if (list.size()==1) {
            System.out.println("暂无数据");
            return;
        }
        System.out.println("-------------------------------------------");
        System.out.println("编号\t名字\t语文\t数学\t英语\t总分");
        TreeSet<Student> scores = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num1 = s2.SumScore() - s1.SumScore();
                int num2 = num1 == 0 ? s2.getChinese() - s1.getChinese() : num1;
                int num3 = num2 == 0 ? s2.getMath() - s2.getMath() : num2;
                int num4 = num3 == 0 ? s1.getId() - s2.getId() : num3;
                return num4;
            }
        });
        for (Person student : list) {
            if (student.getId() != 0) {
                Student st = (Student) student;
                scores.add(st);
            }
        }

        for (Student s : scores) {
            StringBuilder score = new StringBuilder();
            score.append(s.getId()).append("\t").append(s.getName())
                    .append("\t").append(s.getChinese()).append("\t")
                    .append(s.getMath()).append("\t").append(s.getEnglish())
                    .append("\t").append(s.SumScore());
            System.out.println(score);
        }

    }

//    private void updateScore(ArrayList<Student> studentList) {
//        System.out.println("修改分数成功");
//    }


    private void addScore(ArrayList<Person> list) {
        System.out.println("请输入要录入分数的学生编号:");
        int id = sc.nextInt();
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("编号" + id + "的学生不存在");
            return;
        }
        Student st = (Student) list.get(index);
        System.out.println("-------------------------------------------------");
        System.out.println("编号" + id + "\t" + st.getName() + "\t" + "录入前");
        System.out.println("语文:" + st.getChinese() + "\t"
                + "数学:" + st.getMath() + "\t"
                + "英语:" + st.getEnglish());
        System.out.println("请输入语文分数:");
        st.setChinese(sc.nextInt());
        System.out.println("请输入数学分数:");
        st.setMath(sc.nextInt());
        System.out.println("请输入英语分数:");
        st.setEnglish(sc.nextInt());
        System.out.println("录入成功");


    }

    //编号所在元素在集合中的索引(不存在则返回-1)
    public int getIndex(ArrayList<Person> list, int id) {
            /*
            编号唯一性的判断方法
            遍历list,用id逐个匹配list中每个元素的id,如果匹配到就返回索引
            如果没有匹配到,最后返回-1
             */
        for (int i = 0; i < list.size(); i++) {
            if (id == list.get(i).getId()) {
                return i;
            }
        }
        return -1;
    }
}
